typedef struct arraylist {
  int capacity;
  int size;
  tree *data[0];
}


*arraylist arraylist_create(int capacity) {
  arraylist *l;
  l = (arraylist *)malloc(sizeof(arraylist)+sizeof(tree)*capacity);
  l->capacity = capacity;
  //l->data = malloc();
  l->size = 0;
}

//return item at index
tree *arraylist_get(arraylist *l, int index) {
  return l->data[index];
}

//return index of item (find using binary search)
int arraylist_search(arraylist *l, tree *needle) {
  int mid;
  int low = 0;
  int high = l->size-1;
  
  while (low <= high) {
    mid = (low + high) / 2; //check between upper and lower limits
    if (arraylist_get(l,mid) > needle) {//if needle is smaller than mid
      high = mid-1; //set upper limit just below mid
    } else {
      if (arraylist_get(l,mid) < needle) { //if needle is larger than mid
	low = mid+1; //set lower limit just above mid
      } else { //if needle is not smaller than mid and not larger than mid
	return mid;//found !
      }
    }
  }
  return -1; // not found 
}


//insert item in the end
void arraylist_insert(arraylist *l,tree *obj) {
  (l->data)[l->size] = obj;
  (l->size)++;
}

void arraylist_print(arraylist *l) {
  int size = l->size;
  int index;
  for (index = 0; index <size; index++) {
    printf("data[%d] = %p\n",index,arraylist_get(l,index));
  }
}
